package com.swdegao.iot.netty.udp.server;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;

import com.swdegao.iot.SwdegaoIotMgtServerApplication;
import com.swdegao.iot.netty.entity.MyProps;
import com.swdegao.iot.netty.entity.Parameter;
import com.swdegao.iot.netty.udp.server.channelhandler.HeartBeatClientHandler;
import com.swdegao.iot.netty.udp.server.channelhandler.ParseMessgaeHandler;
import com.swdegao.iot.netty.udp.server.channelhandler.PushMessgaeHandler;
import com.swdegao.iot.netty.udp.server.channelhandler.SaveMessgaeHandler;
import com.swdegao.iot.netty.server.decoder.DG_DATADecoder;

import io.netty.channel.*;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.EventExecutorGroup;

public class NettyServerChannelInitializer extends ChannelInitializer<NioDatagramChannel> {
	private EventLoopGroup pusheventloop = new NioEventLoopGroup();
	//private EventExecutorGroup pusheventloop = new NioEventLoopGroup(); 
	@Autowired
	private MyProps myProps = SwdegaoIotMgtServerApplication.getBean(MyProps.class);

	@Override
	protected void initChannel(NioDatagramChannel socketChannel) throws Exception {

		// UDp
		ChannelPipeline pipeline = socketChannel.pipeline();
		pipeline.addLast(new DG_DATADecoder(1<<20,1,2,-3,0));
		pipeline.addLast(new ParseMessgaeHandler());// 推送
		pipeline.addLast(new SaveMessgaeHandler());// 存储
		
		// socketChannel.pipeline().addLast(new ParseMessgaeHandler());//推送
		// socketChannel.pipeline().addLast(new SaveMessgaeHandler());//存储
		if ("1".equals(myProps.getIscallback())) {// #0:不推送,1:推送
			pipeline.addLast(pusheventloop,new PushMessgaeHandler());
		}

	}
}
